package com.gu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gu.entity.EduCourse;
import com.gu.entity.EduTeacher;
import com.gu.entity.vo.front.TeacherVo;
import com.gu.service.EduCourseService;
import com.gu.service.EduTeacherService;
import entity.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

/**
 * Description: 前台教师管理界面
 *
 * @Autor: Rolin
 * datetime: 06-14-11:20
 */
@RestController
@CrossOrigin
@Api(tags = "前台教师管理界面")
@RequestMapping("/eduService")
public class TeacherFrontController {

    @Resource
    private EduTeacherService eduTeacherService ;

    @Resource
    private EduCourseService eduCourseService ;

    @ApiOperation("分页查询所有教师")
    @GetMapping("/front/getTeachersByPage/{current}")
    public CommonResult getAllTeachersByPage(@PathVariable("current")String current){
        Page<EduTeacher> teacherPage = eduTeacherService.getFrontTeacherList(current);
        List<EduTeacher> teacherList = teacherPage.getRecords();
        //还必须得到总页数：
        long totalPages = teacherPage.getPages();
        //还必须得到总记录数：
        long totalRecords = teacherPage.getTotal();

        List<TeacherVo> list = new ArrayList<>();
        teacherList.forEach(teacher -> {
            TeacherVo teacherVo = new TeacherVo();
            BeanUtils.copyProperties(teacher,teacherVo);
            list.add(teacherVo);
        });

        return list.size()>0 ? CommonResult.success().data("teacherList",list).data("totalPages",totalPages).data("totalRecords",totalRecords): CommonResult.error();
    }

    @ApiOperation("查询教师信息以及所授课程")
    @GetMapping("/front/getTeacherInfoAndCourse/{id}")
    public CommonResult getTeacherInfoAndCourse(@PathVariable("id")String id){
        //getTeacherInfoById
        EduTeacher teacherInfo = eduTeacherService.getById(id);
        //getCourses belong to  teacher
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        eduCourseQueryWrapper.eq("teacher_id",id);
        List<EduCourse> courseList = eduCourseService.list(eduCourseQueryWrapper);

        return (courseList.size()>0 && !StringUtils.isEmpty(teacherInfo.getName())) ?
                CommonResult.success().data("teacherInfo",teacherInfo).data("courseList",courseList):
                CommonResult.error();

    }
}
